package com.open.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.open.common.utils.PageUtils;
import com.open.common.utils.Query;
import com.open.common.utils.R;
import com.open.loan.utils.HttpUtil;
import com.open.loan.utils.sms.SmsTry;
import com.open.system.domain.LoanSignTemplateDO;
import com.open.system.service.LoanSignTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 签名模板
 *
 * @author aaa
 * @email aaax
 * @date 2019-05-22 12:08:49
 */

@Controller
@RequestMapping("/system/loanSignTemplate")
public class LoanSignTemplateController {
    @Autowired
    private LoanSignTemplateService loanSignTemplateService;

    @GetMapping()
    @RequiresPermissions("system:loanSignTemplate:loanSignTemplate")
    String LoanSignTemplate() {
        return "system/loanSignTemplate/loanSignTemplate";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:loanSignTemplate:loanSignTemplate")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<LoanSignTemplateDO> loanSignTemplateList = loanSignTemplateService.list(query);
        int total = loanSignTemplateService.count(query);
        PageUtils pageUtils = new PageUtils(loanSignTemplateList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("system:loanSignTemplate:add")
    String add() {
        return "system/loanSignTemplate/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("system:loanSignTemplate:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        LoanSignTemplateDO loanSignTemplate = loanSignTemplateService.get(id);
        model.addAttribute("loanSignTemplate", loanSignTemplate);
        return "system/loanSignTemplate/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:loanSignTemplate:add")
    public R save(LoanSignTemplateDO loanSignTemplate) {
        loanSignTemplate.setCreateTime(new Date());

        if (StringUtils.isEmpty(loanSignTemplate.getTemplateName())) {
            return R.error("模板名称不能为空");
        }
        if (StringUtils.isEmpty(loanSignTemplate.getContent())) {
            return R.error("模板内容不能为空");
        }

        if (StringUtils.isEmpty(loanSignTemplate.getRemark1())) {
            return R.error("模板场景不能为空");
        }

        String accesskey = "QX7R54xfCtSY7bMx"; //用户开发key
        String accessSecret = "UQ0CCsD2zT1q0EWgryoy83n7R8gXJyvc"; //用户开发秘钥
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("accesskey", accesskey);
        parameters.put("secret", accessSecret);
        parameters.put("templateName", loanSignTemplate.getTemplateName());
        parameters.put("categoryId", "2");
        parameters.put("template", loanSignTemplate.getContent());
        parameters.put("description", loanSignTemplate.getRemark1());
        String returnBack = HttpUtil.sendGet("http://api.1cloudsp.com/open/api/addTemplate", parameters);
        System.out.println(returnBack);
        JSONObject returnBackJson = JSONObject.parseObject(returnBack);
        if (0 == Integer.parseInt(String.valueOf(returnBackJson.get("ret")))) {
            String signIdStr = String.valueOf(returnBackJson.get("data"));
            JSONObject signIdJson = JSONObject.parseObject(signIdStr);
            String templateId = String.valueOf(signIdJson.get("templateId"));
            System.out.println(templateId);
            loanSignTemplate.setTemplateId(Integer.parseInt(templateId));
            loanSignTemplate.setStatus("0");
        } else {
            return R.error();
        }
        if (loanSignTemplateService.save(loanSignTemplate) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("system:loanSignTemplate:edit")
    public R update(LoanSignTemplateDO loanSignTemplate) {
        try {
            new SmsTry().modifyTemplate(String.valueOf(loanSignTemplate.getTemplateId()),
                    loanSignTemplate.getTemplateName(),
                    loanSignTemplate.getContent(),
                    loanSignTemplate.getRemark1()
            );
        } catch (Exception e) {
            e.printStackTrace();
        }
        loanSignTemplateService.update(loanSignTemplate);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:loanSignTemplate:remove")
    public R remove(Long id) {
        if (loanSignTemplateService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:loanSignTemplate:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        loanSignTemplateService.batchRemove(ids);
        return R.ok();
    }

}
